//
//  PopoverAnimator.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 21/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

let GMLPopoverAnimatorWillShow = "GMLPopoverAnimatorWillShow"
let GMLPopoverAnimatorWillDissmiss = "GMLPopoverAnimatorWllDissmiss"

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning
{
    var isPresented: Bool = false
    var presentFrame:CGRect = CGRect.zero

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
    {
        let popVC = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        popVC.presentFrame = presentFrame
        return popVC
    }
    /*
     返回动画时长
     */
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        
        return 0.3
    }
    
    /*
     告诉系统如何动画  无论展示或消失
     */
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // 1 修改原来的transform
        // toView = PopoverViewController.view
        if isPresented {
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
            toView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
            transitionContext.containerView.addSubview(toView)
//            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            UIView.animate(withDuration: 0.3, animations: {
                toView.transform = CGAffineTransform.identity
            },completion: { _ in
                // 这句话一定要添加上去 否则会莫名其妙的出问题
                transitionContext.completeTransition(true)
            })
        }else {
            // fromVC = HomeViewController
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
            UIView.animate(withDuration: 0.3, animations: {
                // y: 0.00001 由于swift的缺陷 写0.0（cgfloat）会没有反应
                fromView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.00001)
                
            }, completion: { _ in
                // 这句话一定要添加上去 否则会莫名其妙的出问题
                transitionContext.completeTransition(true)
            })
        }
    }
    
    /**
     * 告诉系统谁来负责弹出的转场动画
     * 实现这两个方法之后 系统的自带动画就会消失
     * 此时需要手动实现展示和消失的动画
     **/
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresented = true
        // 发送通知  告诉VC 即将展示
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: GMLPopoverAnimatorWillShow), object: nil)
        return self
    }
    /**
     * 告诉系统谁来负责消失的转场动画
     * 实现这两个方法之后 系统的自带动画就会消失
     * 此时需要手动实现展示和消失的动画
     **/
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresented = false
        // 发送通知  告诉VC 即将消失
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: GMLPopoverAnimatorWillDissmiss), object: nil)
        return self
    }
    
 
}
